package view.modifiers;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.enumTypes.CSMoveStyle;
import model.path.CSPlayerPath;

public class PlayerPathModifier extends JPanel implements Modifier, FocusListener {
	private static final String[] moveStrings = {"Run", "Walk", "Crouch", };
	
	private JComboBox movesBox;
	private JTextField waitField;
	
	private CSPlayerPath path;
	private int index;
	
	public PlayerPathModifier(CSPlayerPath path_, int index_) {
		
		path = path_;
		index = index_;
		
		movesBox = new JComboBox(moveStrings);
		waitField = new JTextField(Double.toString(path.getNode(index).getWaitTime()),3);
		
		waitField.addFocusListener(this);
		
		CSMoveStyle style = path.getNode(index).getMoveStyle();
		if (style == CSMoveStyle.RUN)
			movesBox.setSelectedIndex(0);
		if (style == CSMoveStyle.WALK)
			movesBox.setSelectedIndex(1);
		if (style == CSMoveStyle.CROUCH)
			movesBox.setSelectedIndex(2);
		
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
//		this.setBorder(BorderFactory.cre
		this.add(new JLabel("Move style after the node :"));
		this.add(Box.createVerticalStrut(3));
		this.add(movesBox);
		this.add(Box.createVerticalStrut(5));
		this.add(new JLabel("Wait time at the node :"));
		this.add(Box.createVerticalStrut(3));
		this.add(waitField);
	}


	public void modify() {
		CSMoveStyle style;
		switch (movesBox.getSelectedIndex()) {
		case 0:
			style = CSMoveStyle.RUN;
			break;
		case 1:
			style = CSMoveStyle.WALK;
			break;
		case 2:
			style = CSMoveStyle.CROUCH;
			break;
		default:
			style = CSMoveStyle.RUN;
			break;
		}
		
		path.setNodeMoveStyle(index,style);
		try {
			path.waitUntil(index,path.getNode(index).getTime()+Double.parseDouble(waitField.getText()));
			path.setNodeWaitTime(index, path.getNode(index).getWaitTime());
		}
		catch (NumberFormatException e) {}
	}

	public void focusGained(FocusEvent arg0) {
		waitField.selectAll();
	}

	public void focusLost(FocusEvent arg0) {}
}
